package com.sign2024.sign2024project.projectservice.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.BatchNeed;
import com.sign2024.sign2024core.entry.projecttask.ProjectTask;
import com.sign2024.sign2024project.aop.listener.BatchNeedExcelListener;
import com.sign2024.sign2024project.projectservice.param.BatchNeedParam;
import com.sign2024.sign2024project.projectservice.service.BatchNeedService;
import com.sign2024.sign2024project.projecttask.service.ProjectTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 本批需求一览表 控制层
 *
 * @author yujie
 * @data 2024/7/25 15:50
 */
@Api(tags = "工程服务")
@RestController
@RequestMapping("/api/batchNeed")
public class BatchNeedController {

    @Resource
    private BatchNeedService batchNeedService;

    @Resource
    private ProjectTaskService projectTaskService;

    /**
     * 本批需求一览表excel导入
     *
     * @param file
     * @param taskId 任务 id
     * @return
     * @throws IOException
     */
    @Log(title = "工程服务", businessType = "导入")
    @ApiOperation(value = "本批需求一览表excel导入", notes = "本批需求一览表excel导入")
    @Transactional
    @PostMapping("/import")
    public CommonResult<BatchNeed> importInfo(MultipartFile file, Integer taskId) {
        if (StringUtils.isNull(taskId)) {
            throw new CommonException(CommonConstants.ERROR, "任务标识为空");
        }

        // 删除该任务下的数据
        batchNeedService.deleteByTaskId(taskId);
        // 将 任务 id 进行赋值
        BatchNeedExcelListener listener = new BatchNeedExcelListener();
        listener.setTaskId(taskId);
        try {
            EasyExcel.read(file.getInputStream(), listener).head(BatchNeed.class).sheet().doReadSync();

//            // 删除之前数据
//            batchNeedService.deleteBatchNeedTempByTaskId(taskId);
//            // 执行完成 做数据统计
//            batchNeedService.addBatchBatchNeedTemp(taskId);

            // 导入数据后， 将任务中的 本批需求一览表 列修改为 已导入
            ProjectTask projectTask = new ProjectTask();
            projectTask.setId(taskId);
            projectTask.setBatchNeed("已导入");
            projectTaskService.updateTask(projectTask);

            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "工程服务", businessType = "查询")
    @ApiOperation(value = "分页查询本批需求一览表", notes = "分页查询本批需求一览表")
    @PostMapping("/page")
    public CommonResult<BatchNeed> page(BatchNeedParam param) {
        Page<BatchNeed> page = batchNeedService.pageBatchNeed(param);
        return CommonResult.success(page);
    }
}
